package cn.zhutmj_bigmarket.infrastructure.persistent.repository;

import cn.zhutmj_bigmarket.domain.strategy.model.entity.StrategyAwardEntity;
import cn.zhutmj_bigmarket.domain.strategy.model.entity.StrategyEntity;
import cn.zhutmj_bigmarket.domain.strategy.model.entity.StrategyRuleEntity;
import cn.zhutmj_bigmarket.domain.strategy.repository.IStrategyRepository;
import cn.zhutmj_bigmarket.infrastructure.persistent.dao.IStrategyAwardDao;
import cn.zhutmj_bigmarket.infrastructure.persistent.dao.IStrategyDao;
import cn.zhutmj_bigmarket.infrastructure.persistent.dao.IStrategyRuleDao;
import cn.zhutmj_bigmarket.infrastructure.persistent.po.StrategyAwardPo;
import cn.zhutmj_bigmarket.infrastructure.persistent.po.StrategyPo;
import cn.zhutmj_bigmarket.infrastructure.persistent.po.StrategyRulePo;
import cn.zhutmj_bigmarket.infrastructure.persistent.redis.IRedisService;
import cn.zhutmj_bigmarket.types.common.Constants;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description: 仓储实现类
 * @author: zhutmj
 * @createTime: 2025/6/2118:06
 */
@Repository
public class StrategyRepository implements IStrategyRepository {

    @Resource
    private IStrategyAwardDao strategyAwardDao;
    @Resource
    private IStrategyDao strategyDao;
    @Resource
    private IStrategyRuleDao strategyRuleDao;
    @Resource
    private IRedisService redisService;

    /**
     * 获取策略id对应的颧骨策略奖品实体
     * @param strategyID
     * @return 策略奖品列表
     */
    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyID) {
        // 1.首先尝试在缓存中获取列表
        String cacheKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyID;
        List<StrategyAwardEntity> strategyAwardList =  redisService.getValue(cacheKey);
        if(null != strategyAwardList && !strategyAwardList.isEmpty()) {
            return strategyAwardList;
        }
        // 2.从数据库中获取列表
        List<StrategyAwardPo>  awardList = strategyAwardDao.queryStrategyListByStrategyId(strategyID);
        // 从数据库po对象列表中组装对象
        strategyAwardList = new ArrayList<StrategyAwardEntity>(awardList.size());
        for(StrategyAwardPo awardPo : awardList) {
            StrategyAwardEntity strategyAwardEntity = StrategyAwardEntity.builder()
                    .strategyId(strategyID)
                    .awardId(awardPo.getAwardId())
                    .awardCount(awardPo.getAwardCount())
                    .awardCountSurplus(awardPo.getAwardCountSurplus())
                    .awardRate(awardPo.getAwardRate())
                    .build();
        }
        // 3.放进redis缓存
        redisService.setValue(cacheKey, strategyAwardList);
        return strategyAwardList;
    }

    @Override
    public void storeStrategyAwardSearchRateTable(String key, Integer rateRange, Map<Integer, Integer> strategyAwardSearchRateTable) {
        // 1.获取抽奖策略范围值
        redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + key, rateRange);
        // 2.存储概率查找表
        Map<Integer,Integer> cacheRate = redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + key);
        cacheRate.putAll(strategyAwardSearchRateTable);
    }

    @Override
    public Integer getStrategyAwardAssemble(String key, Integer rateKey) {
        return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + key, rateKey);
    }

    @Override
    public int getRateRange(Long strategyId) {
        return getRateRange(String.valueOf(strategyId));
    }

    @Override
    public int getRateRange(String key) {
        return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + key);
    }

    @Override
    public StrategyEntity queryStrategyEntityByStrategyId(Long strategyId) {
        // 优先从缓存中拿
        String cacheKey = Constants.RedisKey.STRATEGY_KEY + strategyId;
        StrategyEntity strategyEntity =  redisService.getValue(cacheKey);
        if(null != strategyEntity) {return strategyEntity;}
        StrategyPo strategy = strategyDao.queryStrategyByStrategyId(strategyId);
        strategyEntity = StrategyEntity.builder()
                .strategyId(strategy.getStrategyId())
                .strategyDesc(strategy.getStrategyDesc())
                .ruleModels(strategy.getRuleModels())
                .build();
        this.redisService.setValue(cacheKey, strategyEntity);
        return strategyEntity;
    }

    @Override
    public StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleModel) {
        StrategyRulePo strategyRuleReq = new StrategyRulePo();
        strategyRuleReq.setStrategyId(strategyId);
        strategyRuleReq.setRuleModel(ruleModel);
        StrategyRulePo strategyRuleRes = strategyRuleDao.queryStrategyRule(strategyRuleReq);
        return StrategyRuleEntity.builder()
                .strategyId(strategyRuleRes.getStrategyId())
                .awardId(strategyRuleRes.getAwardId())
                .ruleType(strategyRuleRes.getRuleType())
                .ruleDesc(strategyRuleRes.getRuleDesc())
                .ruleModel(strategyRuleRes.getRuleModel())
                .ruleValue(strategyRuleRes.getRuleValue())
                .build();
    }

    @Override
    public String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel) {
        StrategyRulePo strategyRule = new StrategyRulePo();
        strategyRule.setStrategyId(strategyId);
        strategyRule.setAwardId(awardId);
        strategyRule.setRuleModel(ruleModel);
        return strategyRuleDao.queryStrategyRuleValue(strategyRule);
    }

}
